package com.ncloud.resman.controller;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.busi.DeviceExcelBusi;
import com.ncloud.resman.busi.DeviceLocationBusi;
import com.ncloud.resman.busi.DeviceServerBusi;
import com.ncloud.resman.entity.DeviceLocation;
import com.ncloud.resman.service.DeviceLocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by shiqiang on 2017/8/15.
 */
@Api(value = "自动化运维用户管理", description = "设备防火墙信息", tags = "设备防火墙信息",
        basePath="/",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/resman/device")
public class DeviceFirewallController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DeviceServerBusi deviceServerBusi;
    @Autowired
    private DeviceExcelBusi deviceExcelBusi;

    /*新增设备服务器*/
    @ApiOperation(value="创建防火墙", notes="创建防火墙")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "X-CSRF-TOKEN" ,required = true,dataType = "String")
    })
    @PostMapping(value = "/firewall")
    public Result<Object> saveFirewall(HttpServletRequest request, @RequestBody JSONObject json) throws Exception {
        logger.info("防火墙报文信息："+json.toJSONString());
        Object userID = request.getAttribute("userID");
        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }else{
            json.put("userID",Long.parseLong(userID.toString()));
        }
        deviceServerBusi.addDeviceSever(json);
        return ResultUtil.success();
    }

    /*交换机信息查询*/
    @GetMapping(value = "/firewall/{deviceId}")
    public Result<Object> getFirewallInfo(@PathVariable("deviceId") Long deviceId) throws Exception {
        return ResultUtil.success(deviceServerBusi.getDeviceSwitchInfo(deviceId));
    }

    /*交换机信息更新*/
    @PutMapping(value = "/firewall")
    public Result<Object> modifyFirewall(@RequestBody JSONObject json,
                                       HttpServletRequest request) throws Exception {
        Object userID = request.getAttribute("userID");
        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }else{
            json.put("userID",Long.parseLong(userID.toString()));
        }
        deviceServerBusi.modifyDeviceSever(json);
        return ResultUtil.success();
    }
  //导出Excel
    @GetMapping(value = "/fire/export")
    public Result<Object> exportServerInfo(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	String snNum=request.getParameter("snNum");
    	deviceExcelBusi.exportFireInfos(snNum, response,true);
    	return ResultUtil.success();
    }
    //导出Excel模板
    @GetMapping(value = "/fire/exportTemplate")
    public Result<Object> exportServerTemplate(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	String snNum=request.getParameter("snNum");
    	deviceExcelBusi.exportFireInfos(snNum, response,false);
    	return ResultUtil.success();
    }    
    //导入Excel
    @PostMapping(value ="/fire/import")
    public Result<?> importServerInfo(HttpServletRequest request, @RequestParam(value="file",required=false) MultipartFile file) throws Exception{
    	Object userID = request.getAttribute("userID");
        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }
    	JSONObject re=deviceExcelBusi.importFireInfo(file.getInputStream(),userID);
    	return ResultUtil.success(re);
    }
}
